package com.speljohan.rsbot.event.events;

import java.util.EventListener;

import com.speljohan.rsbot.accessors.Character;
import com.speljohan.rsbot.accessors.NPC;
import com.speljohan.rsbot.accessors.Player;
import com.speljohan.rsbot.event.EventMulticaster;
import com.speljohan.rsbot.event.listeners.CharacterMovedListener;
import com.speljohan.rsbot.script.wrappers.RSCharacter;
import com.speljohan.rsbot.script.wrappers.RSNPC;
import com.speljohan.rsbot.script.wrappers.RSPlayer;

public class CharacterMovedEvent extends RSEvent {
	private static final long serialVersionUID = 8883312847545757405L;
	private final Character character;
	private final int direction;

	private RSCharacter wrapped;

	public CharacterMovedEvent(final Character character, final int direction) {
		this.character = character;
		this.direction = direction;
	}

	@Override
	public void dispatch(final EventListener el) {
		((CharacterMovedListener) el).characterMoved(this);
	}

	public RSCharacter getCharacter() {
		if (wrapped == null) {
			if (character instanceof NPC) {
				final NPC npc = (NPC) character;
				wrapped = new RSNPC(npc);
			} else if (character instanceof Player) {
				final Player player = (Player) character;
				wrapped = new RSPlayer(player);
			} else {
				wrapped = new RSCharacter(character);
			}
		}
		return wrapped;
	}

	/**
	 * <alowaniak> 0 = N-W <alowaniak> 1 = north <alowaniak> 2 = N-E <alowaniak>
	 * 3 = west <alowaniak> 4 = east <alowaniak> 5 = S-W <alowaniak> 6 = south
	 * <alowaniak> 7 = S-E
	 * */
	public int getDirection() {
		return direction;
	}

	@Override
	public long getMask() {
		return EventMulticaster.CHARACTER_MOVED_EVENT;
	}
}
